<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\AdminConfig;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Redis;

class CacheController extends Controller
{

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('清除缓存')->description('清除缓存')->body($this->form());
	}


	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$defaultSetting = [
			'is_clear_cache' => 0,
		];

		$form = new Form($defaultSetting);

		$form->action('cache');

		$form->radio('is_clear_cache', '清除缓存')->options([
			'0' => '不是',
			'1' => '是'
		])->default('0');

		return $form;
	}


	public function store(Request $request)
	{
		$data = $request->all();

		if ($data['is_clear_cache'] == 1){
			Artisan::call('page-cache:clear');
			Redis::DEL('index_content');
			Redis::DEL('headData');
			admin_toastr('清除缓存成功！');
		}else{
			admin_toastr('无需清除！');
		}

		return back();
	}
}
